package com.zjk.toolkit.dynamic.thread.pool.sdk.registry.redis;

import com.zjk.toolkit.dynamic.thread.pool.sdk.domain.entity.ThreadPoolEntity;
import com.zjk.toolkit.dynamic.thread.pool.sdk.domain.enums.RegistryEnum;
import com.zjk.toolkit.dynamic.thread.pool.sdk.registry.IRegistry;
import lombok.RequiredArgsConstructor;
import org.redisson.api.RBucket;
import org.redisson.api.RList;
import org.redisson.api.RedissonClient;

import java.time.Duration;
import java.util.List;

@RequiredArgsConstructor
public class RedisRegistry implements IRegistry {

    private final RedissonClient redissonClient;
    @Override
    public void reportThreadPool(List<ThreadPoolEntity> threadPoolEntityList) {
        RList<ThreadPoolEntity> threadPoolConfigList = redissonClient.getList(RegistryEnum.THREAD_POOL_CONFIG_PARAMETER_LIST_KEY.getKey());
        threadPoolConfigList.delete();
        threadPoolConfigList.addAll(threadPoolEntityList);
    }

    @Override
    public void reportThreadPoolConfigParamter(ThreadPoolEntity threadPoolEntity) {
        String key = RegistryEnum.THREAD_POOL_CONFIG_PARAMETER_LIST_KEY.getKey()
                + "_" + threadPoolEntity.getAppName() + "_"+ threadPoolEntity.getThreadPoolName();
        RBucket<ThreadPoolEntity> bucket = redissonClient.getBucket(key);
        bucket.set(threadPoolEntity, Duration.ofDays(30));
    }
}
